//
//  HelpViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-7-25.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "HelpViewController.h"
#import "NSString+FontAwesome.h"
#import <QuartzCore/QuartzCore.h>
#import "AppUtils.h"

@interface HelpViewController ()

@property (nonatomic, strong)UIButton* closeBtn;

@end

@implementation HelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInteger pages = 5;

    [self.nibPageControl setNumberOfPages:pages];
    

    [self createPages:pages];
    
    
    UIButton* closeBtn= [[UIButton alloc] initWithFrame:CGRectMake(270, 20, 50, 50)];
    [closeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [closeBtn.titleLabel setFont:[UIFont fontWithName:kFontAwesomeFamilyName size:30]];
    [closeBtn setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-times"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closePage:) forControlEvents:UIControlEventTouchUpInside];
    self.closeBtn = closeBtn;
    [self.view addSubview:self.closeBtn];
    
    [self.nibPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
}

- (void)createPages:(NSInteger)pages {
    for (int i = 0; i < pages; i++) {
        
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, 568)];
        
        if ([[AppUtils getLan] isEqualToString:@"FR"]) {
            [imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"g%i_fr.jpg", i]]];
        }else if ([[AppUtils getLan] isEqualToString:@"ENG"]){
            [imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"g%i_en.jpg", i]]];
        }else{
            [imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"g%i.jpg", i]]];
        }
        
        
        [self.scrollView addSubview:imgView];
    }
    
    UIButton* closeBtn= [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth * 4 + 78, ScreenHeight/2 +160, 164, 48)];
    [closeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [closeBtn.titleLabel setFont:[UIFont fontWithName:kFontAwesomeFamilyName size:24]];
    [closeBtn setBackgroundColor:HEX2UIColor(0xffffff)];
    [closeBtn.layer setCornerRadius:6.0];
    [closeBtn setTitle:NSLocalizedString(@"开始体验", nil) forState:UIControlStateNormal];
    [closeBtn setTitleColor:HEX2UIColor(0xabcd03) forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closePage:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:closeBtn];
    
    [self.scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.scrollView.bounds) * pages, CGRectGetHeight(self.scrollView.bounds))];
}


-(void)closePage:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.pageControl maskEventWithOffset:scrollView.contentOffset.x frame:scrollView.frame];
    [self.nibPageControl maskEventWithOffset:scrollView.contentOffset.x frame:scrollView.frame];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = self.scrollView.frame.size.width;
	NSInteger page =  floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    NSLog(@"page is %i",page);
    if (page==4) {
        [self.closeBtn setHidden:YES];
    }else{
        [self.closeBtn setHidden:NO];
    }
    [self.pageControl setCurrentPage:page];
    [self.nibPageControl setCurrentPage:page];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    CGFloat pageWidth = self.scrollView.frame.size.width;
	NSInteger page =  floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    [self.pageControl setCurrentPage:page];
    [self.nibPageControl setCurrentPage:page];
}

#pragma mark - IBActions
- (void)changePage:(KVNMaskedPageControl *)sender {
	self.pageControl.currentPage = sender.currentPage;
	self.nibPageControl.currentPage = sender.currentPage;
	
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:YES];
}

#pragma mark - KVNMaskedPageControlDataSource
- (UIColor *)pageControl:(KVNMaskedPageControl *)control pageIndicatorTintColorForIndex:(NSInteger)index {
    if (index == 0 || index == 3 || index == 4 ) {
        return [UIColor colorWithWhite:1.0 alpha:.6];
    } else {
        return [UIColor colorWithWhite:0 alpha:.5];
    }
}

- (UIColor *)pageControl:(KVNMaskedPageControl *)control currentPageIndicatorTintColorForIndex:(NSInteger)index {
    if (index == 0 || index == 3 || index == 4 ) {
        return nil;
    } else {
        return [UIColor colorWithWhite:0 alpha:.8];
        
    }
}

@end
